package com.mooc.ppjoke.exoplayer;

import android.app.Application;
import android.view.LayoutInflater;

import com.mooc.lib_common.MyApplication;
import com.mooc.ppjoke.R;
import com.google.android.exoplayer2.DefaultLoadControl;
import com.google.android.exoplayer2.DefaultRenderersFactory;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.ui.PlayerControlView;
import com.google.android.exoplayer2.ui.PlayerView;

/**
 * @ClassName: PageListPlay$
 * @Description: java类作用描述
 * @Author: 魏靳元
 * @CreateDate: 2022/8/22 13:42
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public class PageListPlay {
    public SimpleExoPlayer exoPlayer;
    public PlayerView playerView;
    public  PlayerControlView controllerView;
    public String playUrl;
    public PageListPlay() {
        Application application = MyApplication.getsApplication();
        exoPlayer = ExoPlayerFactory.newSimpleInstance(application, new DefaultRenderersFactory(application), new DefaultTrackSelector(), new DefaultLoadControl());
        playerView = (PlayerView) LayoutInflater.from(application).inflate(R.layout.layout_exoplayer, null, false);
        controllerView = (PlayerControlView) LayoutInflater.from(application).inflate(R.layout.layout_exoplayer_contorller_view, null, false);
        playerView.setPlayer(exoPlayer);
        controllerView.setPlayer(exoPlayer);
    }
    public void switchPlayerView(PlayerView newPlayerView, boolean attach) {
        playerView.setPlayer(attach ? null : exoPlayer);
        newPlayerView.setPlayer(attach ? exoPlayer : null);
    }
    public void release() {
        if(exoPlayer!=null){
            exoPlayer.setPlayWhenReady(false);
            exoPlayer.stop(true);
            exoPlayer.release();
            exoPlayer=null;
        }
        if (playerView != null) {
            playerView.setPlayer(null);
            playerView = null;
        }

        if (controllerView != null) {
            controllerView.setPlayer(null);
            controllerView.setVisibilityListener(null);
            controllerView = null;
        }
    }
}
